package com.wn.service;

import com.wn.common.pojo.Destination;
import com.wn.common.pojo.ReqDestination;
import com.wn.common.pojo.Rsp;
import com.wn.mapper.DestinationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;






/**
 * @BelongsProject: hurricane_travel
 * @BelongsPackage: com.wn.service
 * @Author: lhq
 * @Description: 目的地业务层
 */

@Service
@Transactional
public class DestinationService {
    @Autowired
    private DestinationMapper destinationMapper;

    /**
     * 级联查询目的地业务
     * @param reqDestination
     * @return
     */
    public Rsp getDestinationList(ReqDestination reqDestination) {
        if(StringUtils.isEmpty(reqDestination)){
            return new Rsp().fail().msg("请求编号为空");
        }
        return new Rsp().success(destinationMapper.getDestinationByParentId(reqDestination.getParentId()));

    }

    public Rsp getById(Integer destinationId) {
        Destination destination  = destinationMapper.selectByPrimaryKey(destinationId);
        if(StringUtils.isEmpty(destination)){
            return new Rsp().fail();
        }
        return new Rsp().success(destination);
    }


    //搜索出所有有效目的地
    public Rsp getAllDestination() {
        List<Destination> allDestination = destinationMapper.getAllDestination();

        return Rsp.success(allDestination);
    }

    /**
     * 根据父查询出子分支
     * @return
     */
    public List<Destination> getDestinationChild(Destination destination){
        List<Destination> destinationChild = destinationMapper.getDestinationChild(destination);
        return destinationChild;
    }

    /**
     * 查询出目的地以及展示景点的名
     * @return
     */
    public Rsp getDestinationParent() {

        List<Destination> destination = destinationMapper.getDestinationParent();

        if(StringUtils.isEmpty(destination)){
            return Rsp.fail();
        }else{

            for (Destination d: destination) {
                List<Destination> destinationChild = getDestinationChild(d);
                d.setDestinationsList(destinationChild);
            }

            return Rsp.success(destination);
        }

    }

    /**
     * 查询出0级
     * @return
     */
    public Rsp getDestinationSuperior() {

        List<Destination> destination = destinationMapper.getDestinationSuperior();




        if(StringUtils.isEmpty(destination)){
            return Rsp.fail();
        }else{

//            for (Destination d: destination) {
//                List<Destination> destinationChild = getDestinationChild(d);
//                d.setDestinationsList(destinationChild);
//            }

            return Rsp.success(destination);
        }

    }

    /**
     * 查询出0级别的下一级数据
     * @param destination
     */
    public Rsp getDestinationListByParentId(Destination destination) {

        List<Destination> list = destinationMapper.getDestinationListByParentId(destination);

        for (Destination d: list) {
                List<Destination> destinationChild = getDestinationChild(d);
                d.setDestinationsList(destinationChild);
            }

        return Rsp.success(list);

    }
}
